<div id="app">
    <admin-page-header back>
        <bi-button icon="fa-save" @click="save"  type="primary">保存</bi-button>
    </admin-page-header>
    
    <div class="container">
        <bi-form ref="form">
        <div class="row g-2">
            <div class="col-12">
                <bi-card title="分类信息">
                  
                        <bi-form-item label="分类名称" >
                            <bi-input v-model="form.title" placeholder="请输入分类名称" required></bi-input>
                        </bi-form-item>

                        <bi-form-item label="上级分类" >
                            <shop-cate root v-model="form.parent_id" required :disabled-list="disabledList"   ></shop-cate>
                        </bi-form-item>

                        <bi-form-item label="链接" >
                            <bi-input v-model="form.route" placeholder="请输入分类链接" ></bi-input>
                        </bi-form-item>

                        <bi-form-item label="分类模型" >
                            <shop-model v-model="form.cate_model_id" type="cate" ></shop-model>
                        </bi-form-item>
                </bi-card>
            </div>

            <div class="col-12" v-if="form.cate_model_id">
                <bi-card title="分类参数"  >
                    <shop-model-param ref="param" :model="form.cate_model_id"  :data="param" ></shop-model-param>
                </bi-card>
            </div>
            <div class="col-12">
                <bi-card title="商品参数">
                    <bi-form-item label="商品模型" >
                        <shop-model v-model="form.goods_model_id" type="goods" ></shop-model>
                    </bi-form-item>
                    <bi-form-item label="商品规格" >
                        <shop-spec v-model="form.goods_spec_id" ></shop-spec>
                    </bi-form-item>
                    
                </bi-card>
            </div>
            
        </div>
    </bi-form>
    </div>
</div>


<script>
import shopCate from 'field/cate.vue';
import shopModel from 'field/model.vue';
import shopModelParam from 'field/model-param.vue';
import shopSpec from 'field/spec.vue';
let app = admin.createApp({
    components:{
        shopCate,
        shopModel,
        shopSpec,
        shopModelParam,
    },
    data(){

        let form = {
            cate_id:'',
            title:'',
            parent_id:0,
            url:'',
            brief:'',
            content:'',
            cate_model_id:'',
            goods_model_id:'',
            goods_spec_id:''
        }

        let param = {};


        this.$assign(form, this.$pageData.Cate);
        
        let disabledList = [];
        if (form.cate_id!=''){
            disabledList.push(form.cate_id);
        }

        if (this.$pageData.param) param = this.$pageData.param;

        return {
            form,
            param,
            disabledList,
            parentUrl:''
        }
    },
    watch:{
        form:{
            handler(value){
               
            },            
            deep:true,
            immediate:true
        }
    },
    methods:{
        save(){

            let param = {};

            if (this.$refs.form.check()){
 
                if (this.form.cate_model_id=='' ||  this.$refs.param.check()){
                    
                    if (this.$refs.param !=undefined) param = this.$refs.param.getData();

                    this.$ajax({
                    url:this.$url({action:'save'}),
                    data:{
                        ...this.form,
                        param
                    },
                    success:(res)=>{
                            this.$user.delLocalStorage('shop-cate');
                            this.$go(-1);
                        }
                    });

                }
            }

        },
    }
}).mount('#app');
</script>